package cn.tedu.service.impl;

import cn.tedu.mapper.ApplicationFormMapper;
import cn.tedu.mapper.ChapterMapper;
import cn.tedu.mapper.NovelChapterMapper;
import cn.tedu.pojo.ApplicationForm;
import cn.tedu.pojo.Chapter;
import cn.tedu.pojo.NovelChapter;
import cn.tedu.service.ApplicationFormService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cache.annotation.CachePut;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Slf4j
public class ApplicationFormServiceImpl implements ApplicationFormService {
    @Autowired
    private ApplicationFormMapper applicationFormMapper;

    @Autowired
    private ChapterMapper chapterMapper;

    //@Autowired
    //private RedisTemplate redisTemplate;

    @Autowired
    private NovelChapterMapper novelChapterMapper;

    @Override
    //@CachePut(value = "novel_system",key = "#applicationForm.id")
    public void roleAndNovelApplication(ApplicationForm applicationForm) {
        applicationForm.setStatus(false);
        applicationFormMapper.insert(applicationForm);
    }

    @Override
    @Transactional
    public String chapterApplication(Chapter chapter) {
        chapter.setStatus(false).setCreated_time(new Date());
        //插入章节表
        chapterMapper.insert(chapter);
        log.info("{}", chapter.getId());
        //添加章节表和小说的关联
        NovelChapter novelChapter = new NovelChapter();
        novelChapter.setNovelId(chapter.getNovelId()).setNovelChapter(chapter.getId());
        novelChapterMapper.insert(novelChapter);

        //ValueOperations op = redisTemplate.opsForValue();
        //op.set("chapter:"+chapter.getId(),chapter );

        //String novelName1 = (String) op.get("novelName");
        //if (novelName1!=null){
        //    log.info("从缓存中取novelName");
        //    return novelName1;
        //}
        //获取小说名
        String novelName = applicationFormMapper.getNovelName(chapter.getId());
        //op.set("novelName", novelName);
        return novelName;
    }

    @Override
    public ApplicationForm getEditorToAuthor(String author) {
        QueryWrapper<ApplicationForm> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("author", author)
                .eq("status", true);
        ApplicationForm applicationForm = applicationFormMapper.selectOne(queryWrapper);
        return applicationForm;
    }
}
